package com.ffcs.crmd.tsp.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.ctg.itrdc.platform.common.entity.PageInfo;
import com.ffcs.crmd.tsp.admin.entity.TaskConnectInfo;
import com.ffcs.crmd.tsp.admin.service.ITaskConnectInfoService;
import com.ffcs.crmd.tsp.api.dto.client.TspResult;
import com.ffcs.crmd.tsp.api.dto.client.TspTaskConnectInfo;
import com.ffcs.crmd.tsp.client.core.TspClient;
import com.ffcs.crmd.tsp.common.exception.TspException;
import com.ffcs.crmd.tsp.core.util.BeanUtils;

/**
 * 
 * 功能说明:执行端连接信息
 *
 * @author ZHONGFUHUA
 * 
 * @Date 2016年4月26日 下午5:10:02
 *
 *
 * 版本号  |   作者   |  修改时间   |   修改内容
 *
 */
@Service
public class TaskConnectInfoServiceImpl extends AbstractService implements ITaskConnectInfoService{
    
    @Override
    public List<TaskConnectInfo> query(TaskConnectInfo taskConnectInfo) throws TspException {
        try {
            TspTaskConnectInfo tspTaskConnectInfoVO = new TspTaskConnectInfo();
            BeanUtils.applyIf(tspTaskConnectInfoVO, taskConnectInfo, false);
            
            TspClient tspClient = getClient(taskConnectInfo.getClusterName());
            TspResult<TspTaskConnectInfo> tspResult = tspClient.queryTaskConnectInfo(tspTaskConnectInfoVO);
            validateResult(tspResult);
            
            List<TspTaskConnectInfo> tspTaskConnectInfoVOList = new ArrayList<TspTaskConnectInfo>();
            tspTaskConnectInfoVOList.addAll(tspResult.getRetVo().getDataList());
            return BeanUtils.copyList(tspTaskConnectInfoVOList, TaskConnectInfo.class); 
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public PageInfo<TaskConnectInfo> queryPage(TaskConnectInfo taskConnectInfo, int pageNumber, int pageSize) throws TspException {
        try {
            PageInfo<TaskConnectInfo> taskConnectInfoPageInfo = new PageInfo<TaskConnectInfo>();
            if(StringUtils.isBlank(taskConnectInfo.getJobName())){
                return taskConnectInfoPageInfo;
            }
            
            TspTaskConnectInfo tspTaskConnectInfoVO = new TspTaskConnectInfo();
            BeanUtils.applyIf(tspTaskConnectInfoVO, taskConnectInfo, false);
            tspTaskConnectInfoVO.setPageNumber(pageNumber);
            tspTaskConnectInfoVO.setPageSize(pageSize);
            
            TspClient tspClient = tspClientService.getClient(taskConnectInfo.getClusterName());
            TspResult<TspTaskConnectInfo> tspResult = tspClient.queryTaskConnectInfo(tspTaskConnectInfoVO);
            validateResult(tspResult);
            
            com.ffcs.crmd.tsp.api.dto.page.PageInfo<TspTaskConnectInfo> tspTaskConnectInfoPageInfo = tspResult.getRetVo().getPageInfo();
            BeanUtils.applyIf(taskConnectInfoPageInfo, tspTaskConnectInfoPageInfo, false);
            taskConnectInfoPageInfo.setList(BeanUtils.copyList(tspTaskConnectInfoPageInfo.getList(), TaskConnectInfo.class));
            return taskConnectInfoPageInfo;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
}
